let animation = wx.createAnimation({
  duration: 4000, //转盘转动时间
  timingFunction: 'ease-out' //动画效果
})
const app = getApp();
Page({
  data: {
    openid: '',

    CUSTOMER_TEL: '',
    phone: '', //手机号
    rotate: 0, //度数
    turning: false, //是否点击,
    zhunsj: '', //另一个页面带来的数据
    prize: '', //中奖概率
    count: '', //剩余次数
    prizeList: [
      2100, //一等奖 ...
      2400, //二等奖  ...
      2330, //三等奖...
      2610, //参与奖 ...
      2200, //谢谢惠顾 ...
    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    txtlist: '', //翻滚的字幕
    flag: true, //设置节流
    modalHidden: true, //新的图片模态框
    ajaj: '', //获取中奖奖项

    jiang_x: '', //奖品等级
    EMPLOYEE_ID: '',

    ad_img: '', //中奖图片


    session_key: '',



    city: [], //市
    district: [], //区
    L2code: '',
    L3code: '',
    city: [], //市
    district: [], //区
    business: '', //网点
    shi_k: true,
    qu_k: true,
    wang_k: true,

    L3name: '',
    L4name: '',
    dizhitian: true,
    L4code: '',
    value: '',

    huoqushouji: true
  },


  showPopup() {

    if (this.data.prize == 4) {
      this.setData({
        modalHidden: true,
        dizhitian: false,

      });

      console.log(22);
      wx.showModal({
        title: '温馨提示',
        content: '请及时前往就近网点激活领取',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后

          } else { //这里是点击了取消以后
            console.log('用户点击取消')
          }
        }
      })

    } else {
      this.setData({
        modalHidden: true,
        dizhitian: false,
      });
      console.log(11);
    }

  },

  //自定义模态框的取消
  modalCancel: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(222);
  },

  //自定义模态框的确定
  queding: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(111);
  },

  //获取手机号
  getPhoneNumber(e) {
    console.log(e);
    console.log(e.detail.code)
    this.setData({
      code: e.detail.code
    })
    wx.request({
      url: app.globalData.interface+'/post/API_1032_getWeiXinPhone',
      method: 'GET',
      //向后端发送的数据
      data: {
        code: this.data.code //将code发送到后台服务器。
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
        console.log(res.data.phone);
        if (res.data.result != 'success') {
          wx.showToast({
            icon: 'error',
            title: '获取手机号失败',
            duration: 1000
          })
        } else {
          this.setData({
            phone: res.data.phone,
          })
          this.ticket()
        }
      }
    })
  },


  //给后端传手机号
  ticket() {
    var that = this
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2011_savePhoneAndOpenId',
      method: 'POST',
      data: {
        CUSTOMER_TEL: that.data.phone,
        KEY: 'xianjgjKey9999',
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        if (res.data.result == 'success') {
          this.setData({
            huoqushouji: true
          })
          wx.showToast({
            title: '手机号保存成功',
            duration: 1000
          })
        } else {
          wx.showToast({
            icon: 'error',
            title: '保存手机号失败',
            duration: 1000
          })
        }
      }
    })
  },


  authorization() {
    var that = this
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2007_getPhoneByOpenId',
      method: 'POST',
      //向后端发送的数据
      data: {
        OPEN_ID: that.data.openid,
        session_key: that.data.session_key,
        OPEN_UUID: that.data.jiami,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
        console.log(res.data.result);
        if (res.data.result != 'success') {

          console.log('获取失败');
        } else {
          that.setData({
            phone: res.data.data.CUSTOMER_TEL
          })

        }
      }
    })
  },







  start: function () {

    // if (this.data.L3name == '' || this.data.L4name == '') {

    //   this.setData({
    //     dizhitian: false
    //   })

    // } else {
    // console.log(that.data.phone);
    let that = this
    if (that.data.flag) {


      if (that.data.phone == '') {





        wx.showModal({
          title: '温馨提示',
          content: '请点击确定获取手机号即可抽奖',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              that.setData({
                huoqushouji: false
              })

            } else { //这里是点击了取消以后
              console.log('用户点击取消')
            }
          }
        })

      } else {
        // 节流
        that.data.flag = false
        wx.request({
          url: app.globalData.interface+'/shangluo/API_1051_shangluoGuoqingRaffle',
          method: 'POST',
          data: {
            OPEN_ID: that.data.openid,
            session_key: that.data.session_key,
            OPEN_UUID: that.data.jiami,
            USER_ASSETS_IDCARD: that.data.phone,
            // EMPLOYEE_ID: that.data.L4code,

          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res);

            if (res.data.result != 'success') {

              if (res.data.wangdian == 2) {
                wx.showModal({
                  title: '温馨提示',
                  title: res.data.data,
                  // title: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后

                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.shangluoGuoqingUserPrize.VALID == 1) {
                console.log(res.data.shangluoGuoqingUserPrize);

                var shouji = res.data.shangluoGuoqingUserPrize

                wx.showModal({
                  title: '温馨提示',
                  title: '四等奖已激活是否领取',
                  // title: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后
                      wx.navigateTo({
                        url: '../../pages/shangguoqinlinqu/shangguoqinlinqu?data=' + JSON.stringify(shouji),
                      })
                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                    }
                  }
                })


              } else if (res.data.shangluoGuoqingUserPrize.VALID == 2) {
                wx.showModal({
                  title: '温馨提示',
                  title: '您已领取过',
                  // title: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后

                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.wangdian == 0) {
                wx.showModal({
                  title: '温馨提示',
                  title: '您已抽中' + res.data.prize + '等奖，请选择网点领奖',
                  // title: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后
                      that.setData({
                        dizhitian: false
                      })
                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                    }
                  }
                })


              } else if (res.data.wangdian == 1) {

                wx.showModal({
                  title: '温馨提示',
                  title: '您已抽中' + res.data.prize + '等奖，是否更改领取网点',
                  // title: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后
                      that.setData({
                        dizhitian: false
                      })
                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                    }
                  }
                })

              }
            } else {

              that.setData({
                prize: res.data.prize,
              })
              setTimeout(() => {
                that.setData({
                  ajaj: res.data.data,
                  ad_img: res.data.prizeMessage.PRIZE_IMG
                })
              }, 5500)
              const index_ = res.data.prize;
              that.rotate(index_)
            }
          }
        })
        setTimeout(() => {
          that.data.flag = true
        }, 5500)
      }

    }
    // }
  },

  // 转盘代码
  rotate: function (index_) {
    // rotate: function () {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      rdm = this.data.prizeList[index_ - 1]; //直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }

        if (rdm == 2200) {
          // showModal('谢谢惠顾');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2100) {
          // showModal('一等奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2330) {
          // showModal('三等奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2610) {
          // showModal('参与奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2400) {
          // showModal('二等奖');
          this.setData({
            modalHidden: false
          })
        }
      }, 4500)
    }
  },

  // 地区数据
  region() {
    wx.request({
      url: app.globalData.interface+'/ankangdabiao/API_5515_userQueryQuXian',
      method: 'POST',
      data: {
        L2code: 61000026,
        OPEN_ID: this.data.openid,
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          city: res.data.data,
        })

      }
    })
  },


  but_shi() {
    this.setData({
      shi_k: false
    })
  },

  but_xian() {
    // if (this.data.L2code == '') {
    //   wx.showToast({
    //     title: '请先选择市',
    //     icon: 'error',
    //     duration: 2000
    //   })
    // } else {
    this.region()
    this.setData({
      qu_k: false
    })

    // }
  },

  but_w() {

    if (this.data.L3code == '') {
      wx.showToast({
        title: '请先选择区',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.Outlets()
      this.setData({
        wang_k: false
      })
    }

  },


  //市
  buttn_c(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.district);
    console.log(e.currentTarget.dataset.item.L2code);
    this.setData({
      district: e.currentTarget.dataset.item.district,
      Lcode: e.currentTarget.dataset.item.L2code,
      L2name: e.currentTarget.dataset.item.L2name,
      shi_k: true

    })

  },

  buttn_x(e) {
    console.log(e.currentTarget.dataset.item.L3code);
    console.log(e);

    this.setData({
      L3code: e.currentTarget.dataset.item.L3code,
      L3name: e.currentTarget.dataset.item.L3name,
      qu_k: true
    })


  },


  buttn_w(e) {
    console.log(e);
    this.setData({
      L4code: e.currentTarget.dataset.item.L4code,
      L4name: e.currentTarget.dataset.item.L4name,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,

      wang_k: true
    })
  },

  //网点
  Outlets() {



    wx.request({
      url: app.globalData.interface+'/ankangdabiao/API_5517_userQueryWangDian',
      method: 'POST',
      data: {
        L3code: this.data.L3code,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data);

        this.setData({
          business: res.data.data,
          wang_k: false
        })


      }
    })
  },

  qu_qd() {

    if (this.data.L3name == '' || this.data.L4name == '') {

      wx.showToast({
        title: '请完善信息',
        icon: 'error',
        duration: 2000
      })

    } else {

      this.setData({
        dizhitian: true

      })
      // wx.showToast({
      //   title: '请点击抽奖',
      //   icon: 'success',
      //   duration: 1000,
      // })
      wx.request({
        url: app.globalData.interface+'/shangluo/API_5305_shangluoGuoqingRaffleSelectWangdian',
        method: 'POST',
        data: {
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
          EMPLOYEE_ID: this.data.EMPLOYEE_ID,
          USER_ASSETS_IDCARD: this.data.phone,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);

          if (res.data.result != 'success') {
            wx.showModal({
              title: '温馨提示',
              title: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后

                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.showModal({
              title: '温馨提示',
              title: '请及时前往' + this.data.L4name + '领奖',
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后

                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })


          }
        }
      })

    }


  },










  onSearch(event) {

    // const n = event.detail

    // this.setData({
    // n:event.detail

    // })

    // console.log(n);
    this.getvidelist()
  },


  onchange(event) {
    console.log(event);
    const value = event.detail
    console.log(value);
    this.setData({
      value: value
    })
  },

  getvidelist(n = '') {
    var that = this
    wx.request({
      url: app.globalData.interface+'/ankangdabiao/API_5518_userQueryCityWangDian',
      method: 'POST',
      data: {


        OPEN_ID: that.data.openid, //用户OPEN_ID
        session_key: that.data.session_key,
        OPEN_UUID: that.data.jiami,
        L2code: 61000026,
        KeyWord: that.data.value,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res', res.data.data);

        if (res.data.data == '') {
          wx.showModal({
            title: '暂无信息',
            content: '请重新搜索',
          });

          this.setData({
            wang_k: true,
            value: '',
          })
        } else {

          that.setData({
            city: res.data.data,
            wang_k: false
          })
          console.log(111);
        }
      },

    })
  },










































  onLoad: function (e) {

    console.log(e.data)
    this.setData({
      phone: app.globalData.phone,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

    console.log(this.data.phone);
    console.log(this.data.jiami);
    console.log(this.data.openid);
    console.log(this.data.session_key);


    this.region()

    if (this.data.phone == '') {
      this.authorization()
    } else {

      console.log('成功');
    }

  },


  onShow() {

    var that = this

    if (that.data.jiami == '' || that.data.session_key == '') {

      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求

            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              // url: 'http://192.168.3.173:8081/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                  // wx.showToast({
                  //   title: '请获取手机号',
                  //   duration: 1000
                  // })
                } else {
                  var openid = res.data.openid;
                  app.globalData.openid = openid;

                  console.log(res.data.openid);
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()

                  that.setData({
                    phone: app.globalData.phone,

                    jiami: app.globalData.jiami,
                    OPEN_ID: app.globalData.openid,
                    openid: app.globalData.openid,
                    session_key: app.globalData.session_key,
                  })
                  console.log(that.data.phone);
                  console.log(that.data.jiami);
                  console.log(that.data.openid);
                  console.log(that.data.session_key);
                }

              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {

      console.log('成功');
    }





  },




})


wx.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})